package com.ygqh.baby.controller.mobile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.PromotionType;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.model.YgShoppingCarModel;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgShoppingCarService;
import com.ygqh.baby.service.YgUserService;
import com.ygqh.baby.service.impl.DocumentConstant;
import com.ygqh.baby.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车相关
 *
 * @author guohao
 */
@Controller
@RequestMapping("/mobile/shoppingcar")
public class YgShoppingCarController {

    @Autowired
    private YgShoppingCarService shoppingCarService;
    @Autowired
    private DocumentConstant documentConstant;
    @Autowired
    private YgUserService ygUserService;

    /**
     * 获取购物车列表
     *
     * @param queryInfo
     * @param callback
     * @return
     */
    @RequestMapping(value = "findShoppingCarList", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findShoppingCarList(QueryInfo queryInfo, @RequestParam(defaultValue = "false") Boolean distribution, String callback) {
        YgUser user = SessionUtil.getCurrentUser();
        List<YgShoppingCarModel> list = shoppingCarService.findShoppingCarList(queryInfo, user.getId(), distribution);
        List<List<YgShoppingCarModel>> carList = shoppingCarService.sortCarList(list, user.getId());
        Message msg = Message.success(documentConstant.MESSAGE_SUCCESS, carList);
        JSONPObject json = new JSONPObject(callback, msg);

        return json;
    }

    /**
     * 获取购物车列表
     *
     * @param queryInfo
     * @param callback
     * @return
     */
    @RequestMapping(value = "findShoppingCarListV2", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findShoppingCarListV2(QueryInfo queryInfo, @RequestParam(defaultValue = "false") Boolean distribution, String callback) {
        YgUser user = SessionUtil.getCurrentUser();

        List<YgShoppingCarModel> list = shoppingCarService.findShoppingCarList(queryInfo, user.getId(), distribution);
        List<List<YgShoppingCarModel>> carList = shoppingCarService.sortCarListV2(list, user.getId());
        Message msg = Message.success(documentConstant.MESSAGE_SUCCESS, carList);
        JSONPObject json = new JSONPObject(callback, msg);

        return json;
    }

    /**
     * 添加普通商品到购物车
     *
     * @param skuId
     * @param quantity
     * @param callback
     * @return
     */
    @RequestMapping(value = "addOrdinaryItem", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject addOrdinaryItem(@RequestParam Long skuId,
                                       @RequestParam Long quantity, String callback) {
        YgUser user = SessionUtil.getCurrentUser();
        Message msg = null;
        if (quantity == null || quantity <= 0) {
            return new JSONPObject(callback, Message.error("亲，请选择购买数量。", null));
        }
        Boolean newCus = shoppingCarService.hasOtherNewCus(skuId, user.getId());
        if (newCus) {
            return new JSONPObject(callback, Message.error("同颜色仅限1件享受新客价", null));
        }
        int row = shoppingCarService.addOrdinaryItem(skuId, quantity, user.getId());
        if (row > 0) {
            msg = Message.success(documentConstant.MESSAGE_SUCCESS, row);

        } else {
            msg = Message.error("操作失败，库存不足！", row);
        }

        return new JSONPObject(callback, msg);
    }

    /**
     * 添加普通商品到购物车
     *
     * @param skuId
     * @param quantity
     * @param callback
     * @return
     */
    @RequestMapping(value = "addOrdinaryItemNew", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject addOrdinaryItemNew(Long skuId, Long quantity, String callback) {
        YgUser user = SessionUtil.getCurrentUser();
        Message msg;
        if (quantity == null || quantity <= 0) {
            return new JSONPObject(callback, Message.error("亲，请选择购买数量。", null));
        }
        int row = shoppingCarService.addOrdinaryItem(skuId, quantity, user.getId());
        if (row > 0) {
            msg = Message.success(documentConstant.MESSAGE_SUCCESS, row);

        } else {
            msg = Message.error("操作失败，库存不足！", row);
        }

        JSONPObject json = new JSONPObject(callback, msg);

        return json;
    }

    /**
     * 添加 加价购或满赠商品到购物车
     *
     * @param skuId
     * @param pType
     * @param promotionPrice
     * @param callback
     * @return
     */
    @RequestMapping(value = "addPromotionItem", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject addPromotionItem(Long skuId, PromotionType pType, BigDecimal promotionPrice, String callback) {
        YgUser user = SessionUtil.getCurrentUser();

        Message msg = shoppingCarService.addPromotionItem(skuId, 1l, user.getId(), pType, promotionPrice);
        if (msg.getType().equals(Message.Type.success)) {

            YgShoppingCarModel carModel = shoppingCarService.findShoppingCarById((Long) msg.getExtra());
            msg.setExtra(carModel);
        }
        JSONPObject json = new JSONPObject(callback, msg);

        return json;
    }

    /**
     * 修改商品数量
     *
     * @param skuId
     * @param isAdd
     * @param callback
     * @return
     */
    @RequestMapping(value = "updateNum")
    @ResponseBody
    public JSONPObject updateNum(Long skuId, Boolean isAdd, String callback) {
        Message msg = null;
        YgUser user = SessionUtil.getCurrentUser();
        int row = shoppingCarService.updateNum(skuId, isAdd, user.getId());
        if (row > 0) {
            msg = Message.success(documentConstant.MESSAGE_SUCCESS, row);

        } else {
            msg = Message.error(documentConstant.MESSAGE_ERROR, row);

        }

        JSONPObject json = new JSONPObject(callback, msg);

        return json;
    }

    /**
     * 获取购物车中商品总数量
     *
     * @param callback
     * @return
     */
    @RequestMapping(value = "findCount")
    @ResponseBody
    public JSONPObject findItemCount(String callback) {
        Message msg = null;
        YgUser user = SessionUtil.getCurrentUser();
        int count = shoppingCarService.findItemCount(user.getId());
        msg = Message.success(documentConstant.MESSAGE_SUCCESS, count);

        JSONPObject json = new JSONPObject(callback, msg);

        return json;
    }

    /**
     * 删除 购物车中商品
     *
     * @param carId
     * @param callback
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public JSONPObject deleteShoppingCar(Long carId, String callback) {
        Message msg = null;
        YgUser user = SessionUtil.getCurrentUser();
        int row = shoppingCarService.deleteSoft(carId, user.getId());
        if (row > 0) {
            msg = Message.success(documentConstant.MESSAGE_SUCCESS, null);

        } else {
            msg = Message.error(documentConstant.MESSAGE_ERROR, null);
        }
        JSONPObject json = new JSONPObject(callback, msg);

        return json;
    }

    /**
     * 清空购物车
     *
     * @param callback
     * @return
     */
    @RequestMapping("removeShoppingCar")
    @ResponseBody
    public JSONPObject removeShoppingCar(String callback) {
        YgUser user = SessionUtil.getCurrentUser();

        Message msg = null;
        int row = shoppingCarService.removeShoppingCar(user.getId());
        if (row > 0) {
            msg = Message.success(documentConstant.MESSAGE_SUCCESS, null);

        } else {
            msg = Message.error(documentConstant.MESSAGE_ERROR, null);
        }
        JSONPObject json = new JSONPObject(callback, msg);

        return json;
    }

}
